*Set up
clear all
set memory 5g

set more off

use "\\ead02\ead_uquam\Localization\NAICS6_panel\restat_final.dta", clear

cd "\\ead02\ead_uquam\Localization\restat_results"


*TSET FUNCTION;

destring naics, replace
destring oecd80, replace

xtset naics year, delta(1)

//Additional variables 

*Relative nafta and asia trade shares

**Imports

gen relnaftashrm = m_naftashr/m_asiashr

summ relnaftashrm, detail

gen relnafta25m=r(p25)
gen relnafta75m=r(p75)

gen relnafta25dm = 0
replace relnafta25dm = 1 if relnaftashrm<relnafta25m

gen relnafta75dm = 0
replace relnafta75dm = 1 if relnaftashrm>relnafta75m

gen lnav_klems_rel25dm = lnav_klems_resid*relnafta25dm
gen lnav_klems_rel75dm = lnav_klems_resid*relnafta75dm


**Exports

gen relnaftashrx = x_naftashr/x_asiashr

summ relnaftashrx, detail

gen relnafta25x=r(p25)
gen relnafta75x=r(p75)

gen relnafta25dx = 0
replace relnafta25dx = 1 if relnaftashrm<relnafta25x

gen relnafta75dx = 0
replace relnafta75dx = 1 if relnaftashrm>relnafta75x

gen lnav_klems_rel25dx = lnav_klems_resid*relnafta25dx
gen lnav_klems_rel75dx = lnav_klems_resid*relnafta75dx


**Port distance and Eastern share


//Standard model at 50km + port distance or relative exports/imports

xi: reg ln_cdf50 lnav_klems_resid ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 lnportdist i.year, cluster(naics)
outreg2 using Trade, ctitle("Portdist: cross")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) replace

xi: xtreg ln_cdf50 lnav_klems_resid ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl m_asiashr m_oecdshr m_naftashr x_asiashr x_oecdshr x_naftashr lnl_idist_n5 lnl_odist_n5 lndistn5 lnportdist i.year, fe cluster(naics)
outreg2 using Trade, ctitle("Portdist: panel")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

xi: reg  ln_cdf50 lnav_klems_resid lnav_klems_rel25dx lnav_klems_rel75dx relnafta25dx relnafta75dx ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl lnl_idist_n5 lnl_odist_n5 lndistn5 i.year, cluster(naics)
outreg2 using Trade, ctitle("Cross - Relative x")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

test lnav_klems_rel25dx-lnav_klems_rel75dx==0

xi: xtreg  ln_cdf50 lnav_klems_resid lnav_klems_rel25dx lnav_klems_rel75dx relnafta25dx relnafta75dx ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl lnl_idist_n5 lnl_odist_n5 lndistn5 i.year, fe cluster(naics)
outreg2 using Trade, ctitle("Panel - Relative x")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

test lnav_klems_rel25dx-lnav_klems_rel75dx==0
test relnafta25dx -relnafta75dx==0

xi: reg  ln_cdf50 lnav_klems_resid lnav_klems_rel25dm lnav_klems_rel75dm relnafta25dm relnafta75dm ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl lnl_idist_n5 lnl_odist_n5 lndistn5 i.year, cluster(naics)
outreg2 using Trade, ctitle("Cross - Relative m")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

test lnav_klems_rel25dm-lnav_klems_rel75dm==0

xi: xtreg  ln_cdf50 lnav_klems_resid lnav_klems_rel25dm lnav_klems_rel75dm relnafta25dm relnafta75dm ln_empl lnherfent lnm_emp nmulti1 nfown lnnrs lnpee lnifqh3shr lnrdl lnl_idist_n5 lnl_odist_n5 lndistn5 i.year, fe cluster(naics)
outreg2 using Trade, ctitle("Panel - Relative m")  sideway alpha (0.01,0.05,0.10) symbol(a,b,c) stats(coef aster se) noparen excel e(all) append

test lnav_klems_rel25dm-lnav_klems_rel75dm==0
test relnafta25dm -relnafta75dm==0
*end
